﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BCCommon.TMS.FreightEnums;
using BCCommon.TMS.Order;
using BCDto.MallInterface;
using BCDto.TMS.MallInterface;
using BCDto.TMS.Order.Order;

namespace BCService.TMS.MallInterface.OrderAsTo
{
    public class MallOrderTransf
    {
        public OrderShippingCostRequestDto TransfCastOrder(MallOrderDto mallOrderDto)
        {
            OrderShippingCostRequestDto dto = new OrderShippingCostRequestDto();
            dto.SourceType = OrderSourceType.Mall;
            dto.FreightType = FreightType.General;
            dto.VehicleTypeId = mallOrderDto.VehicleTypeId;

            dto.GoodsChecklist = new List<OrderShippingCostGoodsRequestDto>();

            if (mallOrderDto.MallGoodsList == null || !mallOrderDto.MallGoodsList.Any())
                throw new ArgumentException("订单中没有商品清单!");

            foreach (var item in mallOrderDto.MallGoodsList)
            {
                var requestDto = new OrderShippingCostGoodsRequestDto(); 
                requestDto.GoodsName = item.GoodsName;
                requestDto.GoodsName = item.GoodsPhoto;
                requestDto.GoodsSku = item.GoodsSKU;
                requestDto.GoodsUnit = item.GoodsUnit;
                requestDto.GoodsPrice = item.GoodsPrice;
                requestDto.GoodsCount = item.GoodsQuantity;
                requestDto.GoodsWeight = item.GoodsWeight;
                requestDto.GoodsVolume = item.GoodsVolume;
                requestDto.GoodsLength = item.GetLength();
                requestDto.GoodsWidth = item.GetWidth();
                requestDto.GoodsHeight = item.GetHight();
                dto.GoodsChecklist.Add(requestDto);
            }

            MallOrderGoodsDto  goods = mallOrderDto.GetMaxLengthGoods();
            dto.GoodsLength = goods.GetLength();
            dto.GoodsWidth = goods.GetWidth();
            dto.GoodsHeight = goods.GetHight();
            dto.GoodsWeight = mallOrderDto.GetTotalGoodsWeight(); 
            dto.GoodsVolume = mallOrderDto.GetTotalGoodsVolume(); 


            dto.IsInsuredPrice = mallOrderDto.IsInsuredPrice;
            dto.InsuredPrice = mallOrderDto.InsuredPrice;
            dto.CountyCode = mallOrderDto.Sender.CountyCode;
            dto.Address = mallOrderDto.Sender.Address;
            dto.Latitude = mallOrderDto.Sender.Lat;
            dto.Longitude = mallOrderDto.Sender.Lng;

            dto.ConsigneeAddress = new List<OrderShippingCostAddressRequestDto>();
            OrderShippingCostAddressRequestDto orderShippingCostAddressRequestDto = new OrderShippingCostAddressRequestDto();
            orderShippingCostAddressRequestDto.EndCountyCode = mallOrderDto.Receiver.CountyCode;
            orderShippingCostAddressRequestDto.EndLatitude = mallOrderDto.Receiver.Lat;
            orderShippingCostAddressRequestDto.EndLongitude = mallOrderDto.Receiver.Lng;
            orderShippingCostAddressRequestDto.EndAddress = mallOrderDto.Receiver.Address;
            orderShippingCostAddressRequestDto.GoodsWeight = mallOrderDto.GetTotalGoodsWeight();
            orderShippingCostAddressRequestDto.GoodsVolume = mallOrderDto.GetTotalGoodsVolume();

            dto.ConsigneeAddress.Add(orderShippingCostAddressRequestDto);

            return dto;
        }
         
    }
}
